const doubleEngrGroupLocEnum = new Array();
const threeEngrGroupLocEnum = new Array();
import sql from './sql'

//初始化
(function () {

    // 枚举2进制 000111-> 111000之间所有组合
    for (let i = 0b000111; i <= 0b111000; i++) {
        let str = i.toString(2).padStart(6, '0') //得到6位二进制数字符串
        let strArr = [...str]          //结构得到单字符的数组
        let groupArr = strArr.map(e => {
            return Number(e)
        }); //转为数字

        //过滤掉不是3个1 3个0的组合
        let zeroNum = 0;
        let oneNum = 0;
        groupArr.some(e => {
            if (e == 0) {
                zeroNum++;
            } else {
                oneNum++;
            }
        });

        if (zeroNum == 3 && oneNum == 3) {
            doubleEngrGroupLocEnum.push(groupArr);
        }
    }

    // 枚举3进制 001122 -> 221100之间所有组合
    for (let i = 44; i <= 684; i++) {
        let str = i.toString(3).padStart(6, '0')
        let strArr = [...str]
        let groupArr = strArr.map(e => {
            return Number(e)
        });

        //过滤掉不是2个2 2个1 2个0的组合
        let num1 = 0, num2 = 0, num0 = 0;
        groupArr.some(e => {
            if (e == 0) {
                num0++;
            } else if (e == 1) {
                num1++;
            } else {
                num2++;
            }
        });

        if (num1 == 2 && num2 == 2 && num0 == 2) {
            threeEngrGroupLocEnum.push(groupArr);
        }
    }

    // console.log("doubleEngrGroupLocEnum", doubleEngrGroupLocEnum);
    // console.log("threeEngrGroupLocEnum", threeEngrGroupLocEnum);
})()

function calc2(engrs, weightsObject, func) {
    console.log("calc2",engrs,weightsObject)
    if (!engrs || !engrs.length || engrs.length < 2) {
        throw new Error("刻印套信息不足2!");
    }

    for (let i = 0; i <= 1; i++) {
        let e = engrs[i];
        if (!e.length || e.length < 6) {
            console.log(e);
            throw new Error("刻印套信息不足6件!");
        }

        e.forEach(e1 => {
            if (!e1.affix1 || !e1.affix2 || !e1.name || !e1.icon) {
                console.log(e1);
                throw new Error("刻印信息缺失!");
            }
        });
    }

    if (!weightsObject) {
        throw new Error("权重表缺失!");
    }

    let weights = weightsObject.weights;


    //开始计算
    doubleEngrGroupLocEnum.forEach((list,calcIndex) => {
        //list是排列顺序 [0,0,0,1,1,1] 
        let result = {
            id:sql.generateUUID(), //生成一个id
            favorited:false, //记录是否收藏了
            compared:false,  //记录是否加入对比了
            mark: 0,
            remark:null,
            index: calcIndex+1,  //方案序列号
            name: '方案'+(calcIndex+1),
            count: {},
            weightId:weightsObject.id,
            weightName:weightsObject.name,
            list: [],
        }
        list.forEach((engrsIndex, index) => {
            //engrIndex 在engrs[]取哪个
            //index 在engr中取哪个位置的刻印

            let engrGroup = engrs[engrsIndex]; //取到刻印组 (6件)
            let engr = engrGroup[index];    //取到指定位置的刻印
            result.list.push(engr);        //记录顺序

            let affix1 = engr.affix1;
            let affix2 = engr.affix2;
            result.mark += weights[affix1];
            result.mark += weights[affix2];

            //词条添加进统计里
            if (result.count[affix1]) {
                result.count[affix1] += 1;
            } else {
                result.count[affix1] = 1;
            }

            if (result.count[affix2]) {
                result.count[affix2] += 1;
            } else {
                result.count[affix2] = 1;
            }

        });

        result.mark /= 10;
        //@TODO serialize  保存格式

        //计算完一条 调用func
        func(result);
    });
}

function calc3(engrs, weightsObject, func) {
    if (!engrs || !engrs.length || engrs.length < 3) {
        throw new Error("刻印套信息不足3!");
    }

    for (let i = 0; i <= 2; i++) {
        let e = engrs[i];
        if (!e.length || e.length < 6) {
            console.log(e);
            throw new Error("刻印套信息不足6件!");
        }

        e.forEach(e1 => {
            if (!e1.affix1 || !e1.affix2 || !e1.name || !e1.icon) {
                console.log(e1);
                throw new Error("刻印信息缺失!");
            }
        });
    }

    if (!weightsObject) {
        throw new Error("权重表缺失!");
    }

    let weights = weightsObject.weights;

    //开始计算
    threeEngrGroupLocEnum.forEach((list,calcIndex)=>{
        let result = {
            id:sql.generateUUID(), //生成一个id
            favorited:false, //记录是否收藏了
            compared:false,  //记录是否加入对比了
            index: calcIndex+1,  //方案序列号
            name: '方案'+(calcIndex+1),
            mark: 0,
            remark:null,
            weightId:weightsObject.id,
            weightName:weightsObject.name,
            count: {},
            list: [],
        }

        list.forEach((engrsIndex, index) => {
            //engrIndex 在engrs[]取哪个
            //index 在engr中取哪个位置的刻印

            let engrGroup = engrs[engrsIndex]; //取到刻印组 (6件)
            let engr = engrGroup[index];    //取到指定位置的刻印
            result.list.push(engr);        //记录顺序

            let affix1 = engr.affix1;
            let affix2 = engr.affix2;
            result.mark += weights[affix1];
            result.mark += weights[affix2];

            //词条添加进统计里
            if (result.count[affix1]) {
                result.count[affix1] += 1;
            } else {
                result.count[affix1] = 1;
            }

            if (result.count[affix2]) {
                result.count[affix2] += 1;
            } else {
                result.count[affix2] = 1;
            }

        });

        result.mark /= 10;
        //@TODO serialize  保存格式

        //计算完一条 调用func
        func(result);
    });
}


export default {

    //开始计算
    //@param num 刻印数量
    //@param callbackFunc(obj) 得到一条结果的回调触发器
    //@param endCallbackFunc() 计算结束回调触发器
    //@param errCallbackFunc(msg) 发生错误时回调函数
    //@param engrs   刻印表
    //@param weights 权重表
    calc_start(num, engrs, weightsObject,callbackFunc, endCallbackFunc, errorCallbackFunc) {
        console.log('calc_start',weightsObject);

        // weightsObject = {
        //     weights:weight,
        //     id:0,
        //     name
        // }

        //权重表转换成字符索引
        let weight = {}

        weightsObject.weights.forEach(e => {
           weight[e.name] = e.weight * 10;
        })
        weightsObject.weights = weight;

        try {
            if (num == 2) {
                calc2(engrs, weightsObject, callbackFunc);
                endCallbackFunc();
                console.log('calc_start end');
            } else {
                calc3(engrs, weightsObject, callbackFunc);
                endCallbackFunc();
                console.log('calc_start end');
            }
        } catch (error) {
            console.log(error);
            errorCallbackFunc(error.message);
        }
    }
}